chrome.extension.onMessage.addListener(
    function (request, sender, sendResponse) {

        // 自动填充用户名密码自动提交跳转
        window.userName = '付会超';
        window.auditOther = false;
        window.modelid = 126;
        if (location.href.indexOf('www.cdtye.com:83/Login.aspx') != -1) {
            $('#txt_Username').val('fuhc');
            $('#txt_Password').val('fhc...763854696');
            $('#btn_Login').click();
            return;
        }

        var type = request.type;
        var url = '';
        if (type === 'add') {
            url = 'https://www.cdtye.com:83/OASystem/PersonalOffice/WorkdailyEdit.aspx?Deptid=1001&OptType=ADD&Modelid=' + modelid;
        } else if (type === 'list') {
            url = 'https://www.cdtye.com:83/OASystem/PersonalOffice/WorkdailyList.aspx?Modelid=' + modelid;
        } else if (type === 'countJB') {
            url = 'https://www.cdtye.com:83/HRManages/SummaryReport/OverWorkSubsidySummaryList.aspx?Modelid=' + modelid;
        } else if (type === 'readMail') {
            url = 'https://www.cdtye.com:83/OASystem/MailManages/ReceiveMailList.aspx?Modelid=' + modelid;
        } else if (type === 'readMsg') {
            url = 'https://www.cdtye.com:83/OASystem/MessageManage/ReceiveMessageList.aspx?Modelid=' + modelid;
        }

        if (window.location.href.indexOf(url) == -1) {
            $('.ahoireportchromeplugindiv').remove();

            $('body').append('<div class="ahoireportchromeplugindiv" style="z-index:99999;position:absolute;left:5%;top:5%;font-size:60px;color:red;background-color:#fff;' +
                'border:5px solid #000;border-radius:10px;text-align:center;">检测到本页面不是ERP系统<br>正在跳转.</div>');

            setInterval(function () {
                var $tips = $('.ahoireportchromeplugindiv');
                $tips.html($tips.html() + '.');
            }, 500);

            setTimeout(function () {
                location.href = url;
            }, 2000);
            return;
        }

        // 一键填报
        if (type === 'add') {
            var workDate = request.workDate;
            var params = request.params;

            $('[name$=txt_Projectnamecode]').val(params.name);
            $('[name$=txt_Projectid]').val(params.id);
            $('[name$=txt_Workdate]').val(workDate);
            $('[name$=txt_Workhouse]').val(params.gs);
            $('[name$=txt_Worklocation]').val(params.dd);
            $('[name$=txt_Workcontent]').val(params.nr);
            $('[name$=txt_Projectfollow]').val(params.gj);
            $('[name$=txt_Projectmanager]').val(params.zg);
            $('[name$=txt_Projectmanagerid]').val(params.zgid);
            $('[name$=txt_Createtime]').val(workDate + ' 17:30:00');
            $('[name$=txt_Memo]').val(params.bz);
            $('[name$=txt_WeekplanContent]').val('无关联个人周计划功能');

            var sucH = '<div style="font-size:40px;">' + workDate + '日报注入成功！等待页面刷新后即可填写下一日期！</div>';
            $('#desktop').append(sucH);

            setTimeout(function () {
                $('#NavContent_btn_Save').click();
            }, 1200);
        } else if (type === 'list') {
            var iframeDivCss = {
                'position': 'absolute',
                'left': '50%',
                'top': '50%',
                'background-color': '#fff',
                'border': '6px solid #1d9c6d',
                'border-radius': '5px'
            };
            $('#ahoireportchromepluginiframeDiv').remove();
            $('body').append(
                '<div id="ahoireportchromepluginiframeDiv" style="display: none;">' +
                '   <iframe id="ahoireportchromepluginiframe" src=""></iframe>' +
                '   <script type="text/javascript">' +
                '   function openss(obj) {' +
                '       $(obj).parents("tr").addClass("active").siblings().removeClass("active");' +
                '       var id = $(obj).parents("tr").find("td:eq(0) span").text();' +
                '       var flowId = $(obj).parents("tr").find("td:last span:eq(0)").text();' +
                '       var flowStatue = $(obj).parents("tr").find("td:last span:eq(1)").text();' +
                '       var auditUserName = $(obj).parents("tr").find("td:last a").text();' +
                '       var $iframeDiv = $("#ahoireportchromepluginiframeDiv");' +
                '       $iframeDiv.css({' +
                '           "margin-left": -$iframeDiv.width() / 2,' +
                '           "margin-top": -$iframeDiv.height() / 2' +
                '       });' +
                '       hideAllBtn();' +
                '       $(".active").find(".ahoireportchromepluginYJSS").hide();/*一键送审*/' +
                '       $(".active").find(".ahoireportchromepluginYJSH").hide();/*一键审核*/' +
                '       $(".active").find(".ahoireportchromepluginGB").show();/*关闭*/' +
                '       var ss_url = "https://www.cdtye.com:83/OASystem/PersonalOffice/WorkdailyEdit.aspx?Modelid=' + modelid + '&Flowid=0&Flowstatue=0&ID={ID}&OptType=EDIT";' +
                '       if(auditUserName.indexOf("' + window.userName + '")>-1){' +
                '           ss_url = "https://www.cdtye.com:83/OASystem/PersonalOffice/WorkdailyEdit.aspx?Modelid=' + modelid + '&Flowid={FLOWID}&Flowstatue={FLOWSTATUE}&AuditEmpid=1285&ID={ID}&OptType=edit";' +
                '           $(".active").find(".ahoireportchromepluginSH").show();/*审核*/' +
                '           $(".active").find(".ahoireportchromepluginFS").show();/*审核*/' +
                '           window.auditOther = true;' +
                '       }else{' +
                '           $(".active").find(".ahoireportchromepluginSS").show();/*送审*/' +
                '           window.auditOther = false;' +
                '       }' +
                '       $("#ahoireportchromepluginiframe").attr("src", ss_url.replace("{ID}", id).replace("{FLOWID}", flowId).replace("{FLOWSTATUE}", flowStatue));' +
                '       $("#ahoireportchromepluginiframeDiv").show();' +
                '   }' +
                '   function hideIframe(){' +
                '       $("#ahoireportchromepluginiframeDiv").hide();' +
                '       hideAllBtn();' +
                '   }' +
                '   function hideAllBtn(){' +
                '       $(".ahoireportchromepluginSS").hide();' +
                '       $(".ahoireportchromepluginSH").hide();' +
                '       $(".ahoireportchromepluginFS").hide();' +
                '       $(".ahoireportchromepluginGB").hide();' +
                '       if(window.auditOther){$(".ahoireportchromepluginYJSH").show();}' +
                '       else{$(".ahoireportchromepluginYJSS").show();}' +
                '   }' +
                '   function sendAudit(){' +
                '       $("#ahoireportchromepluginiframe").contents().find("#NavContent_btn_Save").click();' +
                '       hideIframe();' +
                '       $(".active").find(".ahoireportchromepluginYJSS").css("color", "#fff").css("background-color", "#008000").removeAttr("onclick").text("已送审");' +
                '   }' +
                '   function passAudit(){' +
                '       $(".ahoireportchromepluginFS").change();' +
                '       $("#ahoireportchromepluginiframe").contents().find("#MainContent_txt_AuditContent").val("经检查无误，审核通过！");' +
                '       $("#ahoireportchromepluginiframe").contents().find("#MainContent_btn_Agree").click();' +
                '       hideIframe();' +
                '       $(".active").find(".ahoireportchromepluginYJSH").css("color", "#fff").css("background-color", "#008000").removeAttr("onclick").text("已通过");' +
                '   }' +
                '   function changeFS(obj){' +
                '       $("#ahoireportchromepluginiframe").contents().find("#MainContent_ddl_Finishquality").val($(obj).val());' +
                '   }' +
                '   </script>' +
                '</div>');
            var $iframeDiv = $('#ahoireportchromepluginiframeDiv');
            var $iframe = $('#ahoireportchromepluginiframe');
            var width = $(window).width() * 0.7;
            var height = $(window).height() - 200;
            $iframe.width(width).height(height);
            $iframeDiv.width(width).height(height).css(iframeDivCss);
            $('#MainContent_kgvw_WorkdailyList tr.dgHead td').eq(1).text('此列送审');
            $('#MainContent_kgvw_WorkdailyList tr:not(.dgHead)').each(function (i, e) {
                var $td = $(e).find('td').eq(1);
                if ($(e).find('input[type=checkbox]').length > 0) {
                    $td.last().html('');
                    $td.append('<div class="ahoireportchromepluginYJSS" onclick="openss(this)" style="border-radius: 3px;cursor:pointer;width: 100%;height: 30px;background-color: #f00;color: #fff;text-align: center;line-height: 30px;font-size: 16px;">一键送审</div>');
                    $td.append('<div class="ahoireportchromepluginSS" onclick="sendAudit();" style="border-radius: 3px;display:none;float:left;cursor:pointer;width: 45%;height: 30px;background-color: #1d9c6d;color: #fff;text-align: center;line-height: 30px;font-size: 16px;">送审</div>');
                } else {
                    if ($(e).find('td:last a').text().indexOf(window.userName) > -1) {
                        $td.last().html('');
                        var s = '';
                        for (var ii = 1; ii <= 10; ii++) {
                            s += '<option ' + (ii === 8 ? "selected" : "") + ' value="' + ii + '">' + ii + ' 分</option>';
                        }
                        $td.append('<div class="ahoireportchromepluginYJSH" onclick="openss(this)" style="border-radius: 3px;cursor:pointer;width: 100%;height: 30px;background-color: #f00;color: #fff;text-align: center;line-height: 30px;font-size: 16px;">一键审核</div>');
                        $td.append('<select class="ahoireportchromepluginFS" onchange="changeFS(this)" style="margin-bottom: 3px;display:none;border: none;background-color: #1d9c6d;color: #fff;width: 100%;cursor: pointer;height: 20px;float:left;">' + s + '</select>');
                        $td.append('<div class="ahoireportchromepluginSH" onclick="passAudit();" style="border-radius: 3px;display:none;float:left;cursor:pointer;width: 45%;height: 30px;background-color: #1d9c6d;color: #fff;text-align: center;line-height: 30px;font-size: 16px;">审核</div>');
                    }
                }
                $td.append('<div class="ahoireportchromepluginGB" onclick="hideIframe();" style="border-radius: 3px;display:none;float:right;cursor:pointer;width: 45%;height: 30px;background-color: #ca8c00;color: #fff;text-align: center;line-height: 30px;font-size: 16px;">关闭</div>');
            });
            $('#ahoireportchromepluginiframeTip').remove();
            $('#desktop').append('<div id="ahoireportchromepluginiframeTip" style="font-size:40px;color:red;">☝☝☝点击【一键送审】或【一键审核】按钮☝☝☝</div>');
        } else if (type === 'countJB') {
            var total = 0;
            $.each($('#MainContent_kgvw_OverworkList tr:not(.dgHead)'), function (i, e) {
                total += parseFloat($(e).find('td:eq(4)').text())
            });
            var start = $('#SearchContent_txt_Planoverworkstartdatetime').val();
            var end = $('#SearchContent_txt_Planoverworkenddatetime').val();
            alert(start + ' 至 ' + end + ' 总计加班：' + total + ' 天');
        } else if (type === 'readMail') {
            $('[id=LinkMailsubject]').each(function (i, e) {
                $('body').append('<iframe style="position: absolute;left:-9999px;top:-9999px;" src="' + $(e).attr('href') + '"></iframe>');
            });
            $('body').append('<div style="position:absolute;left:100px;top:100px;font-size:60px;color:red;background-color:#fff;' +
                'border:5px solid #000;border-radius:10px;text-align:center;">本页已读完。' +
                '<br><span id="ahoireportchromepluginTimeDown">3</span>秒后刷新剩余未读。' +
                '<br><span style="font-size: 40px">文字消失后继续点击【一键读邮件】</span></div>');

            setInterval(function () {
                var timer = $('#ahoireportchromepluginTimeDown').text();
                timer = parseInt(timer) - 1;
                if (timer >= 0) {
                    $('#ahoireportchromepluginTimeDown').text(timer);
                }
            }, 1000);

            setTimeout(function () {
                $('#SearchContent_cbx_Isreadmail_0').prop('checked', true);
                $('#SearchContent_cbx_Isreadmail_1').prop('checked', false);
                $('#SearchContent_btn_Search').click();
            }, 2000);
        } else if (type === 'readMsg') {
            var readMsgUrl = 'https://www.cdtye.com:83/OASystem/MessageManage/ReceiveMessageEdit.aspx?Modelid=' + modelid + '&OptType=VIEW&ID=';
            $('[id*=MainContent_kgvw_ReceiveMessageList_lbl_ID_]').each(function (i, e) {
                $('body').append('<iframe style="position: absolute;left:-9999px;top:-9999px;" src="' + readMsgUrl + $(e).text() + '"></iframe>');
            });
            $('body').append('<div style="position:absolute;left:100px;top:100px;font-size:60px;color:red;background-color:#fff;' +
                'border:5px solid #000;border-radius:10px;text-align:center;">本页已读完。' +
                '<br><span id="ahoireportchromepluginTimeDown">3</span>秒后刷新剩余未读。' +
                '<br><span style="font-size: 40px">文字消失后继续点击【一键读消息】</span></div>');

            setInterval(function () {
                var timer = $('#ahoireportchromepluginTimeDown').text();
                timer = parseInt(timer) - 1;
                if (timer >= 0) {
                    $('#ahoireportchromepluginTimeDown').text(timer);
                }
            }, 1000);

            setTimeout(function () {
                $('#SearchContent_cbx_Readstatus_0').prop('checked', true);
                $('#SearchContent_cbx_Readstatus_1').prop('checked', false);
                $('#SearchContent_btn_Search').click();
            }, 2000);
        }
    }
);


